package com.globallogic.training.basics;

import java.util.*;

public class Anagram {
	
	private static final int OFFSET_UPPER_TO_LOWER_CASE = 32;
	
	private static final String ONLY_ONE_CHARACTER_IS_ENTERED = "Only one character is entered";
	
	private static boolean isCorrectInput(char[] userCharacters, StringBuilder outputWrong) {
		for (char userCharacter : userCharacters) {		
			if (!((userCharacter >= 'a' && userCharacter <= 'z') || (userCharacter >= 'A' && userCharacter <= 'Z') ||
				 (userCharacter == ' '))) {
				outputWrong.append(userCharacter);
				return false;
			}
		}
		
		return true;
	}
	
	private static char[] processInput(char[] userCharacters) {
		char[] processedUserCharacters = new char[0];
		
		for (char userCharacter : userCharacters) {		
			if (userCharacter >= 'a' && userCharacter <= 'z') {
				processedUserCharacters = addCharToArray(processedUserCharacters, userCharacter);
			} else if (userCharacter >= 'A' && userCharacter <= 'Z') {
				processedUserCharacters = addCharToArray(processedUserCharacters, (char)((int)userCharacter + OFFSET_UPPER_TO_LOWER_CASE));
			}
		}
		
		return processedUserCharacters;
	}
	
	private static boolean isAnagram(char[] firstChars, char[] secondChars) 
	{
		if (firstChars.length != secondChars.length) {
			return false;
		}
	
		firstChars = arrangeCharArray(firstChars);
		secondChars = arrangeCharArray(secondChars);
		
		for (int index = 0; index < firstChars.length; index++) {
			if (firstChars[index] != secondChars[index]) {
				return false;
			}
		}
		
		return true;
	}
	
	private static char[] arrangeCharArray(char[] array) 
	{
		for (int firstIndex = 0; firstIndex < array.length - 1; firstIndex++) {		
			for (int secondIndex = firstIndex + 1; secondIndex < array.length; secondIndex++) {
				if (array[firstIndex] > array[secondIndex]) {
					char buffer = array[firstIndex];
					array[firstIndex] = array[secondIndex];
					array[secondIndex] = buffer;
				}
			}
		}
		
		return array;
	}
	
	private static char[] addCharToArray(char[] array, char number)
	{
		array = Arrays.copyOf(array, array.length + 1);				
		array[array.length - 1] = number;		
		return array;
	}
	
	private static String convertIndexToWord(int index)
	{
		switch (index) {
			case 1: 
				return "the first";
				
			case 2:
				return "second";
			
			case 3:
				return "third";
			
			default:
				return index + "th";
		}
	}
	
	private static String getErrorMessageNotAllowedCharacter(char wrongCharacter) {
	
		return String.format("Character %c is not allowed in string", wrongCharacter);
	}
	
	public static void printWelcomeMessage() {
		System.out.println("");
		System.out.println("This program tells whether two strings are anagrams");	
	}
	
	public static char[] inputPreparedArray(int indexOfString) {
		Scanner input = new Scanner(System.in);
		
		while (true) {			
			System.out.println("");			
			System.out.printf("Enter %s string (or q to exit): ", convertIndexToWord(indexOfString));
			
			String userInput = input.nextLine();
		
			char[] userCharacters = userInput.toCharArray();
			char[] processedUserCharacters = new char[0];
			StringBuilder outputWrong = new StringBuilder();
			
			if (isCorrectInput(userCharacters, outputWrong)) {
				processedUserCharacters = processInput(userCharacters);
				
				if (processedUserCharacters.length == 1 && (processedUserCharacters[0] == 'q' || processedUserCharacters[0] == 'Q')) {
					return new char[0];
				} else if (processedUserCharacters.length == 1) {						
					System.out.println(ONLY_ONE_CHARACTER_IS_ENTERED);
					
					continue;
				} else {
					return processedUserCharacters;
				}		
			} else {
				System.out.println(getErrorMessageNotAllowedCharacter(outputWrong.charAt(0)));
				
				continue;
			}					
		}
	}
	
	private static void interactWithUser() {		
		while (true) {				
			char[] preparedUserCharacters1 = inputPreparedArray(1);
			
			if (preparedUserCharacters1.length == 0) {
				return;
			}
			
			char[] preparedUserCharacters2 = inputPreparedArray(2);
			
			if (preparedUserCharacters2.length == 0) {
				return;
			}
			
			System.out.println("");
				
			if (isAnagram(preparedUserCharacters1, preparedUserCharacters2)) {
				System.out.println("The strings are anagrams");
			} else {
				System.out.println("The strings are not anagrams");
			}
		}
	}	
	
	public static void main(String[] args) {	
		printWelcomeMessage();
		
		interactWithUser();
	}
}